package lib

func init() {
	Probs = append(Probs, Problem{
		Num:         151,
		Discription: "反转字符串中单词的顺序，只保留合法的空格",
		Level:       2,
		Labels: map[string]int{
			"双指针滑动": 1,
		},
	})
}

func ReverseWords(s string) string {
	left := 0
	right := 1
	isInWord := false
	str := ""
	for right <= len(s) {
		ch := s[right-1]
		if ch == ' ' {
			if isInWord {
                //注意right要+1
				str = s[left:right-1] + " " + str
				left = right
				isInWord = false
			}
			right++
			continue
		}

		if !isInWord {
            left = right - 1
			isInWord = true
		} 
        right++
	}

    //注意遍历完可能处于inword的状态
	if isInWord {
        //注意right要+1
		str = s[left:right-1] + " " + str
	}

	return str[:len(str)-1]
}
